package com.tanhua.admin.controller;

import com.tanhua.admin.service.CommentService;
import com.tanhua.admin.service.ManagerService;
import com.tanhua.admin.service.MovementService;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.MovementAdminVo;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/manage")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @Autowired
    private MovementService movementService;

    @Autowired
    private CommentService commentService;


    @GetMapping("/users")
    public ResponseEntity users(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = managerService.findAllUsers(page,pagesize);
        return ResponseEntity.ok(result);
    }

    //根据id查询
    @GetMapping("/users/{userId}")
    public ResponseEntity findUserById(@PathVariable("userId") Long userId) {
        UserInfo userInfo = managerService.findUserById(userId);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 查询指定用户发布的所有视频列表
     */
    @GetMapping("/videos")
    public ResponseEntity videos(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pagesize,
                                 Long uid ) {
        PageResult result = managerService.findAllVideos(page,pagesize,uid);
        return ResponseEntity.ok(result);
    }

    //查询动态
    @GetMapping("/messages")
    public ResponseEntity messages(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   Long uid,String state) {
        PageResult result = managerService.findAllMovements(page,pagesize,uid,state);
        return ResponseEntity.ok(result);
    }

    //用户冻结
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Map params) {
        Map map =  managerService.userFreeze(params);
        return ResponseEntity.ok(map);
    }

    //用户解冻
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody  Map params) {
        Map map =  managerService.userUnfreeze(params);
        return ResponseEntity.ok(map);
    }

    /**
     * 查询动态详情
     * @param id
     * @return
     */
    @GetMapping("/messages/{id}")
    public ResponseEntity movement(@PathVariable("id") String id){

        MovementAdminVo vo = movementService.findByPid(id);
        return ResponseEntity.ok(vo);
    }

    /**
     * 动态评论列表后台
     * @param page
     * @param pagesize
     * @param messageID 动态id
     * @return
     */
    @GetMapping("/messages/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   String messageID){

        PageResult vo = commentService.pageFindComments(page, pagesize, messageID);
        return ResponseEntity.ok(vo);
    }

    /**
     * pass
     * @param item
     * @return
     */
    @PostMapping("/messages/pass")
    public ResponseEntity pass(@RequestBody String[] item){

        String message = movementService.pass(item);
        Map<String, String> map = new HashMap<>();
        map.put("message", message);
        return ResponseEntity.ok(map);
    }


    @PostMapping("/messages/reject")
    public ResponseEntity reject(@RequestBody String[] item){

        String message = movementService.reject(item);
        Map<String, String> map = new HashMap<>();
        map.put("message", message);
        return ResponseEntity.ok(map);
    }
}
